package com.nhjf.service.impl.system;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nhjf.command.system.CreateRoleCommand;
import com.nhjf.command.system.ModifyRoleCommand;
import com.nhjf.dao.system.RoleDao;
import com.nhjf.model.system.Resource;
import com.nhjf.model.system.Role;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.system.RoleQo;
import com.nhjf.service.impl.BaseServiceImpl;
import com.nhjf.service.system.RoleService;

@Transactional
@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl<Role, RoleQo, RoleDao> implements RoleService {

	@Autowired
	private RoleDao roleDao;
	
	@Override
	protected RoleDao getDao() {
		// TODO Auto-generated method stub
		return roleDao;
	}

	@Override
	public ServiceResultDto createRole(CreateRoleCommand createRoleCommand) {
		
		ServiceResultDto resultDto = new ServiceResultDto();
		Role role=new Role();
		if(createRoleCommand.getName()==null||createRoleCommand.getName().equals("")) {
			
			resultDto.setSuccess(false);
			return resultDto;
		}else {
			role.setName(createRoleCommand.getName());
		}
		if(createRoleCommand.getNumber()!=null) {
			role.setNumber(createRoleCommand.getNumber());
		}
		if(createRoleCommand.getRemark()!=null) {
			role.setRemark(createRoleCommand.getRemark());
		}
		role.setCreateDate(new Date());
		
		if(createRoleCommand.getResourceIdList()==null||createRoleCommand.getResourceIdList().size()==0) {
			
		}else {
			List<Long> idList=createRoleCommand.getResourceIdList();
			Set<Resource> setResource=new HashSet<Resource>();
			for(Long id:idList) {
				Resource re=new Resource();
				re.setId(id);
				setResource.add(re);
			}
			role.setResourceList(setResource);
		}

		roleDao.save(role);
		resultDto.setSuccess(true);
		return resultDto;
	}

	@Override
	public ServiceResultDto modifyRole(ModifyRoleCommand modifyRoleCommand) {
		// TODO Auto-generated method stub
		ServiceResultDto resultDto = new ServiceResultDto();
		Role role=roleDao.get(modifyRoleCommand.getRoleId());
		if(modifyRoleCommand.getName()==null||modifyRoleCommand.getName().equals("")) {
			
			resultDto.setSuccess(false);
			return resultDto;
		}else {
			role.setName(modifyRoleCommand.getName());
		}
		if(modifyRoleCommand.getNumber()!=null) {
			role.setNumber(modifyRoleCommand.getNumber());
		}
		if(modifyRoleCommand.getRemark()!=null) {
			role.setRemark(modifyRoleCommand.getRemark());
		}
		role.setCreateDate(new Date());
		
		if(modifyRoleCommand.getResourceIdList()==null||modifyRoleCommand.getResourceIdList().size()==0) {
			
		}else {
			List<Long> idList=modifyRoleCommand.getResourceIdList();
			Set<Resource> setResource=new HashSet<Resource>();
			for(Long id:idList) {
				Resource re=new Resource();
				re.setId(id);
				setResource.add(re);
			}
			role.setResourceList(setResource);
		}
		roleDao.update(role);
		resultDto.setSuccess(true);
		return resultDto;
	}



}
